//mongodb   数据的操作
//增删改查

const { Db } = require("mongodb");

//show dbs 显示所有的数据库
//use username切换库(没有就新建)
//db.createCollection("music");     在库内建表
//db.username.insert(数据)
//db.tables     查看属于哪个库中的表
//show tables   查看当前库中的表



//查    find    findOne
//增    insert({},[])   insertOne   insertMany  save()
//改    update    updateOne     updateMany
//删除  deleteOne   deleteMany      remove

//增
db.users.insert({username:"刘贤",password:"001",age:120})

db.users.save({username:"阮添",password:"002",age:18})

db.users.insert([{username:"陈创",password:003,age:24},{username:"创创子",password:004,age:25}])

db.users.insertOne({username:"丽丽子",password:005,age:26});

db.users.insertMany([{username:"安林洁",password:006,age:23},{username:"静香",password:007,age:26}])


//改
db.users.update({username:"刘贤"},{$set:{password:001}})

db.users.update({username:"阮添"},{$set:{password:002,age:40}})


//true  查不到就直接插入
db.users.update({username:"谢子豪"},{$set:{password:007,age:19}},true)

//第二个    true 表示修改满足条件的所有数据     updateMany
//      false表示修改满足条件的第一条数据    updateOne   默认为false
db.users.update({},{$set:{adderess:"武汉"}},true,true)


db.users.updateOne({username:"刘贤"},{$set:{age:21}})

db.users.updateMany({},{$set:{word:"18岁不一定快乐"}})

//删除数据      如果数据类型不一样的话  则不能删除
db.users.remove({age:"25"});

//find  查询
db.users.find();
db.users.findOne()  //查第一条数据
db.users.find({age:26})
db.users.findOne({age:26})  //输出第一个满足年龄为26的数据

//0为不显示     1为显示的数据   _id默认为1
db.users.find({username:"阮添"},{_id:0,username:1,word:1})

db.users.find({username:"阮添"},{_id:0,word:0,adderess:0})

//去重查询
db.users.distinct("password")

//模糊查询
//大于20
db.users.find({age:{$gt:20}},{_id:0})

//小于20
db.users.find({age:{$lt:20}},{_id:0})

//大于等于20    小于等于30
db.users.find({age:{$gte:20,$lte:30}},{_id:0})

//$inc可直接在数据上加减   例如；加 age:8   减  age:-8
db.users.updateMany({username:"阮添"},{$inc:{age:-15}}) 

//$or   满足多个条件的
db.users.find({$or:[{age:19},{password:"2"}]},{_id:0})

//模糊查询的正则方法
db.users.find({username:/hua/},{_id:0})

db.users.find({username:/^hua$/},{_id:0})

db.users.find({username:/^hua/},{_id:0})

db.users.find({username:/hua$/},{_id:0})

db.users.find({username:new RegExp('kele')})

db.movise.find({'rating.average': "9.2"})


// 升序 
db.movies.find({},{year:1,title:1,_id:0}).sort({year:1})

// 降序
db.movies.find({},{year:1,title:1,_id:0}).sort({year:-1})

//limit(10)显示的列表数据为10列
db.movies.find({},{'rating.average':1,title:1,_id:0}).sort({'rating.average':-1}).limit(10)

// skip +  limit 可以实现分页  skip跳过前5条
db.movies.find({},{'rating.average':1,title:1,_id:0}).sort({'rating.average':-1}).skip(5).limit(5)

//size只读出满足条件的集合数据
db.movies.find().skip(10).size()        //10

//count读出所有的集合数据
db.movies.find().skip(10).count()       //20

//最大值和最小值(年份最小的和年份最大的)
db.movies.aggregate({$group:{"_id":"year",max_value:{$max:"$year"}}})
db.movies.aggregate({$group:{"_id":"year",min_value:{$min:"$year"}}})

//(评分最高的和最低的)
db.movies.aggregate({$group:{"_id":"rating.average",max_value:{$max:"$rating.average"}}})
db.movies.aggregate({$group:{"_id":"rating.average",min_value:{$min:"$rating.average"}}})

//满足年份为1994和年份为1997的数据
db.movies.find({$or:[{year:"1994"},{year:"1997"}]},{year:1,title:1,_id:0})

//判断如果含有就输出满足条件的数据
db.movies.find({year:{$exists:true}},{year:1,title:1,_id:0})

//判断没有这个数据的话输出为空
db.movies.find({year11:{$exists:true}},{year11:1,title:1,_id:0})

//$in包含这个条件的数据
db.movies.find({genres:{$in:['爱情']}},{title:1,genres:1,_id:0})

//$nin不含这个条件的数据
db.movies.find({genres:{$nin:['爱情']}},{title:1,genres:1,_id:0})